package cn.m3a1.control.admin;

import cn.m3a1.bean.Msg;
import cn.m3a1.bean.StudentOfClass;
import cn.m3a1.bean.User;
import cn.m3a1.service.AdminService;
import cn.m3a1.util.GlobalUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wyj
 * @date 2019/6/29
 * @time 下午8:55
 */
@Controller
@RequestMapping("/admin")
public class AdminHomeController {
    @Autowired
    AdminService adminService;
    @RequestMapping("/home")
    public String  home(@RequestParam(value ="pn",defaultValue = "1")Integer pn,@RequestParam(value ="type",defaultValue = "0")Integer type,String username, ModelMap map){
        PageHelper.startPage(pn,5);
        List<User> users = adminService.getUser(username);
        System.out.println(username);
        //将查询到的结果交给pageInfo,连续显示5个页面的页码
        PageInfo pageInfo=new PageInfo(users,5);
        map.put("members",pageInfo);
        map.put("loginUser",users.get(0));

        return "Admin/AdminHome";
       // return "A";
    }
    @ResponseBody
    @RequestMapping("/getuser")
    public Msg  getuser(int id){

        User user = adminService.getUser(id);
        return Msg.success().add("user",user);

    }

    @ResponseBody
    @RequestMapping("/adduser")
    public Msg addUser(User user){
        user.setPassword(GlobalUtils.md5Encode(user.getPassword()));
        boolean b = adminService.addUser(user);

        if (b){
            return  Msg.success();
        }
        return  Msg.fail();

    }
    @ResponseBody
    @RequestMapping("/deluser")
    public Msg delUser(int id){

        boolean b = adminService.delUser(id);

        if (b){
            return  Msg.success();
        }
        return  Msg.success();

    }
    @ResponseBody
    @RequestMapping("/upuser")
    public Msg upUser(User user){
        user.setPassword(GlobalUtils.md5Encode(user.getPassword()));
        boolean b = adminService.updateUser(user);

        if (b){
            return  Msg.success();
        }
            return  Msg.fail();

    }

    /**
     * 给更改学生的班级
     * @param studentOfClass
     * @return
     */
    @ResponseBody
    @RequestMapping("/setStuClass")
    public Msg setStuClass(StudentOfClass studentOfClass){

        boolean b = adminService.upStuClass(studentOfClass);

        if (b){
            return  Msg.success();
        }
        return  Msg.fail();

    }
}
